/**
 * 项目：洛阳综合信息平台
 * 模块：数据库连接公用类
 * 公司：西安美林电子有限公司
 * 作者：樊锐 2010-8-20
 */

package com.meritit.portal.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.apache.log4j.Logger;

import com.meritit.portal.login.bean.PreferenceBean;

public class DBConnection {

    private static Context initContext = null;

    private static DataSource ds = null;

    private static Logger log = Logger.getLogger(DBConnection.class);

    /**
     * 创建数据库连接
     * 
     * @return
     */
    public static Connection getConn() {
        Connection conn = null;
        String dataSourcAble = PreferenceBean.datasourceEnable;
        if ("false".equals(dataSourcAble)) {
            String driver = PreferenceBean.jdbcDriver;
            String url = PreferenceBean.jdbcUrl;
            String userName = PreferenceBean.jdbcUserName;
            String passWord = PreferenceBean.jdbcPassword;
            try {
                Class.forName(driver);
                conn = DriverManager.getConnection(url, userName, passWord);
            } catch (ClassNotFoundException e) {
                log.error("创建数据库连接异常,请检查驱动:" + e.getMessage(), e);
            } catch (SQLException e) {
                log.error("创建数据库连接异常,请检查数据库服务是否正常:" + e.getMessage(), e);
            }
            return conn;
        } else if("true".equals(dataSourcAble)){
            String dataSourceName = PreferenceBean.jndiName;
            try {
                if (initContext == null) {
                    initContext = new InitialContext();
                }
                if (ds == null) {
                    ds = (DataSource) initContext.lookup(dataSourceName);
                }
                conn = ds.getConnection();
            } catch (NamingException e) {
                log.error("创建数据库链接异常,请检查数据库链接池配置!");
                e.printStackTrace();
            } catch (SQLException e) {
                log.error("创建数据库链接异常,请检查数据库服务器是否正常!");
                e.printStackTrace();
            }
            return conn;
        }else{

            String dataSourceName = PreferenceBean.jndiName;
            try {
                if (initContext == null) {
                    initContext = new InitialContext();
                }
                if (ds == null) {
                    ds = (DataSource) initContext.lookup(dataSourceName);
                }
                conn = ds.getConnection();
            } catch (NamingException e) {
                log.error("创建数据库链接异常,请检查数据库链接池配置!");
                e.printStackTrace();
            } catch (SQLException e) {
                log.error("创建数据库链接异常,请检查数据库服务器是否正常!");
                e.printStackTrace();
            }
            return conn;
        
        }
    }

    /**
     * 关闭数据连接
     * 
     * @param con
     */
    public static void realseCon(Connection con) {
        try {
            if (con != null && !con.isClosed()) {
                con.close();
            }
        } catch (SQLException e) {
            log.error("关闭数据库连接失败!");
            e.printStackTrace();
        }
    }

}
